package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.ThingListAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.base.Constant;
import com.glory.wxelectricitypda.bean.BaseResponseBean;
import com.glory.wxelectricitypda.bean.BomThingBean;
import com.glory.wxelectricitypda.bean.EquipmentBean;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.bean.ResponseQueryBean;
import com.glory.wxelectricitypda.bean.ThingBean;
import com.glory.wxelectricitypda.bean.VendertBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

public class TrackInActivity extends BaseDialogActivity implements BaseQuickAdapter.OnItemChildClickListener {


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.ivAdd)
    ImageView ivAdd;
    @BindView(R.id.rvThingList)
    RecyclerView rvThingList;
    @BindView(R.id.tvCarryId)
    TextView tvCarryId;
    @BindView(R.id.tvLotTitle)
    TextView tvLotTitle;
    @BindView(R.id.ivMainArrow)
    ImageView ivMainArrow;
    @BindView(R.id.ivChildArrow)
    ImageView ivChildArrow;
    @BindView(R.id.llMainEqp)
    LinearLayout llMainEqp;
    @BindView(R.id.llChildEqp)
    LinearLayout llChildEqp;
    @BindView(R.id.llMainEqpParent)
    LinearLayout llMainEqpParent;
    @BindView(R.id.llChildEqpParent)
    LinearLayout llChildEqpParent;
    @BindView(R.id.etMainEqp)
    FreeUI_ClearEditText etMainEqp;
    @BindView(R.id.etChildEqp)
    FreeUI_ClearEditText etChildEqp;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvProvider)
    TextView tvProvider;
    @BindView(R.id.ivProviderArrow)
    ImageView ivProviderArrow;
    @BindView(R.id.llProviderSelect)
    LinearLayout llProviderSelect;
    @BindView(R.id.llProviderItem)
    LinearLayout llProviderItem;
    @BindView(R.id.etGoodsShelves)
    FreeUI_ClearEditText etGoodsShelves;
    @BindView(R.id.llGoodsShelves)
    LinearLayout llGoodsShelves;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.llLotNum)
    LinearLayout llLotNum;
    @BindView(R.id.etCheckGoodsShelves)
    FreeUI_ClearEditText etCheckGoodsShelves;
    @BindView(R.id.llCheckGoodsShelves)
    LinearLayout llCheckGoodsShelves;
    @BindView(R.id.tvTestGoodsShelves)
    TextView tvTestGoodsShelves;
    @BindView(R.id.llTestGoodsShelves)
    LinearLayout llTestGoodsShelves;
    @BindView(R.id.tvGrade)
    TextView tvGrade;
    @BindView(R.id.llGrade)
    LinearLayout llGrade;
    @BindView(R.id.tvGoodsShelvesTitle)
    TextView tvGoodsShelvesTitle;
    @BindView(R.id.llThing)
    LinearLayout llThing;
    @BindView(R.id.etItemCount)
    FreeUI_ClearEditText etItemCount;

    private int conutbars = 0;
    private FreeUI_GeneralPop mainEqpPop, childEqpPop;
    private List<ThingBean> mLots = new ArrayList<>();
    private ThingListAdapter thingListAdapter;
    private FreeUI_GeneralFragmentDialog addCarryIdDialog;
    private String actionType = "BYLOT";
    private FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog;
    private List<EquipmentBean> mainEqpList, childEqpList;
    private List<VendertBean> providerList;
    private ArrayList<String> bomList = new ArrayList<>();
    private ArrayList<String> thingList = new ArrayList<>();
    private FreeUI_GeneralPop providerPop;
    private List<BomThingBean> BOMLINELIST = new ArrayList<>();
    private ResponseQueryBean responseQueryBean;

    @OnClick({R.id.ivAdd, R.id.rlCarryId, R.id.rbLot, R.id.rbWork})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rbLot:
                actionType = "BYLOT";
                tvLotTitle.setText("生产批号：");
                etLotId.setHint("请扫描生产批号");
                llLotNum.setVisibility(View.VISIBLE);
                tvLotNum.setText("");
                etLotId.setText("");

                etChildEqp.setText("");
                etMainEqp.setText("");
                tvProvider.setText("");
                etGoodsShelves.setText("");
                etCheckGoodsShelves.setText("");
                tvStep.setText("");
                tvCarryId.setText("");
                llProviderItem.setVisibility(View.GONE);
                llGoodsShelves.setVisibility(View.GONE);
                llCheckGoodsShelves.setVisibility(View.GONE);
                llTestGoodsShelves.setVisibility(View.GONE);
                llGrade.setVisibility(View.GONE);
                llMainEqpParent.setVisibility(View.GONE);
                llChildEqpParent.setVisibility(View.GONE);
                mainEqpList = null;
                childEqpList = null;
                providerList = null;
                break;
            case R.id.rbWork:
                actionType = "BYWO";
                tvLotTitle.setText("工单号　：");
                etLotId.setHint("请扫描工单号");
                llLotNum.setVisibility(View.GONE);
                tvLotNum.setText("");
                etLotId.setText("");

                etChildEqp.setText("");
                etMainEqp.setText("");
                tvProvider.setText("");
                etGoodsShelves.setText("");
                etCheckGoodsShelves.setText("");
                tvStep.setText("");
                tvCarryId.setText("");
                llProviderItem.setVisibility(View.GONE);
                llGoodsShelves.setVisibility(View.GONE);
                llCheckGoodsShelves.setVisibility(View.GONE);
                llTestGoodsShelves.setVisibility(View.GONE);
                llGrade.setVisibility(View.GONE);
                llMainEqpParent.setVisibility(View.GONE);
                llChildEqpParent.setVisibility(View.GONE);
                mainEqpList = null;
                childEqpList = null;
                providerList = null;
                break;
            case R.id.ivAdd:
                final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
                View view = LayoutInflater.from(TrackInActivity.this).inflate(R.layout.dialog_scan_thing, null);
                TextView tvCancel = view.findViewById(R.id.tvCancel);
                TextView tvConfirm = view.findViewById(R.id.tvConfirm);
                final FreeUI_ClearEditText etThingCode = view.findViewById(R.id.etThingCode);
                final FreeUI_ClearEditText etThingId = view.findViewById(R.id.etThingId);
                etThingCode.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (start == 0 && before == 0 && count > 1) {
                            if (s.toString().contains("|")) {
                                etThingCode.setText(s.toString().replace("|", ",").split(",")[1]);
                            }
                            etThingId.requestFocus();
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });

                etThingId.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (start == 0 && before == 0 && count > 1) {
                            if (s.toString().contains("|")) {
                                etThingId.setText(s.toString().replace("|", ",").split(",")[2]);
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        freeUI_generalFragmentDialog.dismiss();
                    }
                });
                tvConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (etThingCode.getText().toString().trim().length() == 0) {
                            showToast("请扫描物料名称", false);
                            return;
                        }
                        if (etThingId.getText().toString().trim().length() == 0) {
                            showToast("请扫描物料批号", false);
                            return;
                        }
                        setList(etThingCode.getText().toString().trim(), etThingId.getText().toString().trim());
                        freeUI_generalFragmentDialog.dismiss();
                    }
                });
                freeUI_generalFragmentDialog.setLayout(view);
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "11");
                break;
            case R.id.rlCarryId:
                addCarryIdDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描载具编号", new FreeUI_TextCallback() {
                    @Override
                    public void onSuccess(String s) {
                        String carryIds = tvCarryId.getText().toString().trim();
                        boolean isSame = false;
                        if (carryIds.length() == 0) {
                            tvCarryId.setText(s);
                        } else {
                            if (carryIds.contains(";")) {
                                String[] array = carryIds.split(";");
                                for (String str : array) {
                                    if (str.equals(s)) {
                                        isSame = true;
                                    }
                                }
                            } else {
                                if (carryIds.equals(s)) {
                                    isSame = true;
                                }
                            }
                            if (isSame) {
                                showToast("载具编号不能重复", false);
                            } else {
                                StringBuffer stringBuffer = new StringBuffer();
                                stringBuffer.append(carryIds);
                                stringBuffer.append(";");
                                stringBuffer.append(s);
                                tvCarryId.setText(stringBuffer.toString());
                            }
                        }
                        addCarryIdDialog.dismiss();
                    }
                });
                addCarryIdDialog.show(getSupportFragmentManager(), "addCarryIdDialog");
                break;
        }
    }

    private void setList(String matCode, String matLotId) {
            String seq = String.valueOf(mLots.size() + 1);
            thingList.add(matCode);
            mLots.add(new ThingBean(seq, matCode, matLotId));
            thingListAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.llProviderSelect, R.id.llMainEqp, R.id.llChildEqp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.llProviderSelect:
                if (providerList == null) {
                    showToast("还未获取到供应商列表,请重新扫描批次", false);
                    return;
                }
                if (providerPop == null) {
                    List<String> list = new ArrayList<>();
                    for (VendertBean vendertBean : providerList) {
                        list.add(vendertBean.getCUSTOMERCODE() + "-" + vendertBean.getDESCRIPTION());
                    }
                    providerPop = FreeApi_PopUtils.getNormalPop(this, llProviderSelect.getWidth(), list, ivProviderArrow, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {
                            tvProvider.setText(s);
                        }
                    });
                }
                showTypePop(providerPop, ivProviderArrow, llProviderSelect);
                break;
            case R.id.llMainEqp:
                if (mainEqpList == null) {
                    showToast("还未获取到主设备列表,请重新扫描批次", false);
                    return;
                }
                if (mainEqpPop == null) {
                    List<String> list = new ArrayList<>();
                    for (EquipmentBean equipmentBean : mainEqpList) {
                        list.add(equipmentBean.getEQUIPMENTID());
                    }
                    mainEqpPop = FreeApi_PopUtils.getNormalPop(this, llMainEqp.getWidth(), list, ivMainArrow, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {
                            etMainEqp.setText(s);
                        }
                    });
                }
                showTypePop(mainEqpPop, ivMainArrow, llMainEqp);
                break;
            case R.id.llChildEqp:
                if (childEqpList == null) {
                    showToast("还未获取到子设备列表,请重新扫描批次", false);
                    return;
                }
                if (childEqpPop == null) {
                    List<String> list = new ArrayList<>();
                    for (EquipmentBean equipmentBean : childEqpList) {
                        list.add(equipmentBean.getEQUIPMENTID());
                    }
                    childEqpPop = FreeApi_PopUtils.getNormalPop(this, llChildEqp.getWidth(), list, ivChildArrow, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {
                            etChildEqp.setText(s);
                        }
                    });
                }
                showTypePop(childEqpPop, ivChildArrow, llChildEqp);
                break;
        }
    }

    private void showTypePop(FreeUI_GeneralPop pop, View view, View showForView) {
        if (pop.isShowing()) {
            pop.dismiss();
        } else {
            FreeApi_PopUtils.setArrowShow(view);
            pop.showAsDropDown(showForView, 0, 0);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_trackin;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("进站");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String lotId = etLotId.getText().toString().trim();
                if (lotId.length() == 0) {
                    showToast("请扫描生产批号", false);
                    return;
                }

                if (responseQueryBean == null){
                    showToast("请扫描生产批号", false);
                    return;
                }

                final String carryId = tvCarryId.getText().toString().trim();
                if (carryId.length() == 0 && Constant.IS_CARRIER.equalsIgnoreCase("Y")) {
                    showToast("请扫描载具编号", false);
                    return;
                }
                final String mainEqpId = etMainEqp.getText().toString().trim();
                if (mainEqpId.length() == 0 && llMainEqpParent.getVisibility() == View.VISIBLE) {
                    showToast("请选择主设备", false);
                    return;
                }
                final String childEqpId = etChildEqp.getText().toString().trim();
                if (childEqpId.length() == 0 && llChildEqpParent.getVisibility() == View.VISIBLE) {
                    showToast("请选择子设备", false);
                    return;
                }
                final String providerId = tvProvider.getText().toString().trim();
                if (providerId.length() == 0 && llProviderItem.getVisibility() == View.VISIBLE) {
                    showToast("请选择供应商", false);
                    return;
                }
                final String goodsShelves = etGoodsShelves.getText().toString().trim();
                if (goodsShelves.length() == 0 && llGoodsShelves.getVisibility() == View.VISIBLE) {
                    showToast("请输入货架货位", false);
                    return;
                }
                String checkGoodsShelves = etCheckGoodsShelves.getText().toString().trim();
                if (checkGoodsShelves.length() == 0 && llCheckGoodsShelves.getVisibility() == View.VISIBLE) {
                    showToast("请校验货架货位", false);
                    return;
                }
                if (goodsShelves.length() > 0 && checkGoodsShelves.length() > 0 && !goodsShelves.equals(checkGoodsShelves)) {
                    showToast("校验货架货位不通过，请检查", false);
                    return;
                }
                if (llThing.getVisibility() != View.GONE && thingListAdapter.getData().size() == 0){
                    showToast("请添加物料", false);
                    return;
                }
                if (mLots.size() == 0 && llThing.getVisibility() == View.VISIBLE) {
                    Set<String> set = new HashSet<>(thingList);
                    if (set.size() != thingList.size()) {
                        showToast("物料列表有重复数据，请检查", false);
                        return;
                    }

                    for (String s : thingList) {
                        if (!bomList.contains(s)) {
                            showToast(s + "物料与BOM不符合", false);
                            return;
                        }
                    }

                    for (BomThingBean s : BOMLINELIST) {
                        if (!thingList.contains(s.getMATERIALNAME())) {
                            showToast(s.getMATERIALDESC() + "缺失", false);
                            return;
                        }
                    }
                }
                String userName = etUser.getText().toString();
                if (userName.isEmpty()){
                    showToast("请输入操作人工号",false);
                    return;
                }
                final String itemCount = etItemCount.getText().toString().trim();
                if (itemCount.isEmpty()) {
                    showToast("请输入条数", false);
                    return;
                }
                requestUser1(userName, new CallBack() {
                    @Override
                    public void success() {
                        JSONObject map = new JSONObject();
                        try {
                            map.put("LOTID", lotId);
                            map.put("EQPID", mainEqpId);
                            map.put("SUBEQPID", childEqpId);
                            JSONArray jsonArray = new JSONArray(new Gson().toJson(mLots));
                            map.put("MLOTS", jsonArray);
                            map.put("ACTIONTYPE", actionType);
                            map.put("NUMBEROFBARS", itemCount);
                            if (llProviderItem.getVisibility() == View.VISIBLE) {
                                map.put("RESERVED1", providerId.split("-")[0]);
                            }
                            if (llGoodsShelves.getVisibility() == View.VISIBLE) {
                                map.put("RESERVED3", goodsShelves);
                            }
                            if (llTestGoodsShelves.getVisibility() != View.VISIBLE) {
                                map.put("CARRIERID", carryId);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        NetUtils.requestWithNormalCallBack("EUIT.EUITTRACKIN", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                                new TypeToken<BaseResponseBean<?>>() {
                                }.getType(), getSupportFragmentManager(), this) {
                            @Override
                            public void onSuccess(Response<BaseResponseBean<?>> response) {
                                if (response.body().isSuccess(getApplicationContext(), false)) {
                                    showToast("进站成功", true);
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            TrackInActivity.this.finish();
                                        }
                                    }, 2000);
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<?>> response) {
                                super.onError(response);
                                loadError(response.getException(), "EUIT.EUITTRACKIN");
                            }
                        });
                    }

                    @Override
                    public void fail() {
                        showToast("操作人工号验证失败，请重新输入", false);
                    }

                    @Override
                    public void error() {
                        showToast("连接不到服务器，请重试", false);
                    }
                });
            }
        });
        rvThingList.setLayoutManager(new LinearLayoutManager(TrackInActivity.this));
        rvThingList.setItemAnimator(new DefaultItemAnimator());
        rvThingList.addItemDecoration(new DividerItemDecoration(TrackInActivity.this, 1));

        thingListAdapter = new ThingListAdapter(mLots);
        thingListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        thingListAdapter.isFirstOnly(true);
        rvThingList.setAdapter(thingListAdapter);
        thingListAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void initData() {
        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    mainEqpList = null;
                    childEqpList = null;
                    providerList = null;
                    JSONObject jsonObject = new JSONObject();
                    JSONObject jsonObject1 = new JSONObject();
                    if (actionType.equals("BYLOT")) {
                        try {
                            jsonObject.put("LOTID", s.toString());
                            jsonObject.put("ACTIONTYPE", "GETLOTINFO");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            jsonObject.put("WOID", s.toString());
                            jsonObject.put("ACTIONTYPE", "CHECKBYWOLOTINFO");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    NetUtils.requestQueryLot(jsonObject, new FreeUI_DialogEntityCallBack<BaseResponseBean<ResponseQueryBean>>(
                            new TypeToken<BaseResponseBean<ResponseQueryBean>>() {
                            }.getType(), getSupportFragmentManager(), this) {
                        @Override
                        public void onSuccess(Response<BaseResponseBean<ResponseQueryBean>> response) {
                            if (response.body().isSuccess(getApplicationContext(), true)) {
                                responseQueryBean = response.body().getResponse().getBody();
                                //扫描请求成功重置输入框
                                etChildEqp.setText("");
                                etMainEqp.setText("");
                                tvProvider.setText("");
                                etGoodsShelves.setText("");
                                etCheckGoodsShelves.setText("");
                                FreeApi_LogUtils.saveErrorMessages(responseQueryBean.getLOT().getSTEPDESC(), "返回STEPDESC值");
                                tvStep.setText(responseQueryBean.getLOT().getSTEPDESC());
                                tvLotNum.setText(String.valueOf(responseQueryBean.getLOT().getMAINQTY()));
                                BOMLINELIST = responseQueryBean.getBOMLINELIST();
                                for (BomThingBean s:BOMLINELIST){
                                    bomList.add(s.getMATERIALNAME());
                                }
                                //设备栏位操作
                                mainEqpList = responseQueryBean.getEQUIPMENTLIST();
                                if (mainEqpList.size() > 0) {
                                    llMainEqpParent.setVisibility(View.VISIBLE);
                                } else {
                                    llMainEqpParent.setVisibility(View.GONE);
                                }
                                if (responseQueryBean.getLOT().getMODJUDGEFLAG().equalsIgnoreCase("y")) {
                                    llThing.setVisibility(View.VISIBLE);
                                } else {
                                    llThing.setVisibility(View.GONE);
                                }
                                childEqpList = responseQueryBean.getSUBEQUIPMENTLIST();
                                if (childEqpList.size() > 0) {
                                    llChildEqpParent.setVisibility(View.VISIBLE);
                                } else {
                                    llChildEqpParent.setVisibility(View.GONE);
                                }
                                //提示框
                                String tips = responseQueryBean.getLOT().getLOTCOMMENT();
                                if (tips.trim().length() > 0) {
                                    freeUI_generalFragmentDialog = FreeApi_DialogUtils.getConfirmDialog(getApplicationContext(), "我知道了",
                                            tips, new FreeUI_CommonCallback() {
                                                @Override
                                                public void onSuccess() {
                                                    freeUI_generalFragmentDialog.dismiss();
                                                }
                                            });
                                    freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "freeUI_generalFragmentDialog");
                                }
                                //工步相关栏位操作
                                String workStep = responseQueryBean.getLOT().getSTEPNAME();
                                switch (workStep) {
                                    case "T200":
                                        llProviderItem.setVisibility(View.GONE);
                                        llGoodsShelves.setVisibility(View.GONE);
                                        llCheckGoodsShelves.setVisibility(View.GONE);
                                        llTestGoodsShelves.setVisibility(View.VISIBLE);
                                        llGrade.setVisibility(View.VISIBLE);
                                        tvGoodsShelvesTitle.setText("测试货架号：");
                                        tvGrade.setText(responseQueryBean.getLOT().getGRADE1());
                                        tvTestGoodsShelves.setText(responseQueryBean.getLOT().getATTRIBUTE4());
                                        tvCarryId.setText(responseQueryBean.getLOT().getATTRIBUTE3());
                                        break;
                                    case "A401":
                                    case "A402":
                                        llProviderItem.setVisibility(View.GONE);
                                        llGoodsShelves.setVisibility(View.GONE);
                                        llCheckGoodsShelves.setVisibility(View.GONE);
                                        llTestGoodsShelves.setVisibility(View.VISIBLE);
                                        llGrade.setVisibility(View.GONE);
                                        tvTestGoodsShelves.setText(responseQueryBean.getLOT().getATTRIBUTE4());
                                        tvGoodsShelvesTitle.setText("货架号：");
                                        tvCarryId.setText("");
                                        break;
                                    case "A500":
                                        llProviderItem.setVisibility(View.VISIBLE);
                                        llGoodsShelves.setVisibility(View.GONE);
                                        llCheckGoodsShelves.setVisibility(View.GONE);
                                        llTestGoodsShelves.setVisibility(View.GONE);
                                        llGrade.setVisibility(View.GONE);
                                        providerList = responseQueryBean.getVENDERLIST();
                                        tvCarryId.setText("");
                                        break;
                                    case "T100":
                                        tvCarryId.setText("");
                                        llProviderItem.setVisibility(View.GONE);
                                        llGoodsShelves.setVisibility(View.VISIBLE);
                                        llTestGoodsShelves.setVisibility(View.GONE);
                                        llGrade.setVisibility(View.GONE);
                                        etGoodsShelves.setText(responseQueryBean.getLOT().getATTRIBUTE5());
                                        if (responseQueryBean.getLOT().getATTRIBUTE5().length() > 0) {
                                            llCheckGoodsShelves.setVisibility(View.VISIBLE);
                                            etCheckGoodsShelves.requestFocus();
                                            etCheckGoodsShelves.addTextChangedListener(new TextWatcher() {
                                                @Override
                                                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                                }

                                                @Override
                                                public void onTextChanged(CharSequence s, int start, int before, int count) {
                                                    if (start == 0 && before == 0 && count > 1) {
                                                        if (s.toString().equals(etGoodsShelves.getText().toString().trim())) {
                                                            JSONObject map = new JSONObject();
                                                            try {
                                                                map.put("LOTID", etLotId.getText().toString().trim());
                                                                map.put("CARRIERID", tvCarryId.getText().toString().trim());
                                                                JSONArray jsonArray = new JSONArray(new Gson().toJson(mLots));
                                                                map.put("MLOTS", jsonArray);
                                                                map.put("ACTIONTYPE", actionType);
                                                                map.put("RESERVED3", etGoodsShelves.getText().toString().trim());
                                                            } catch (JSONException e) {
                                                                e.printStackTrace();
                                                            }
                                                            NetUtils.requestWithNormalCallBack("EUIT.EUITTRACKIN", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                                                                    new TypeToken<BaseResponseBean<?>>() {
                                                                    }.getType(), getSupportFragmentManager(), this) {
                                                                @Override
                                                                public void onSuccess(Response<BaseResponseBean<?>> response) {
                                                                    if (response.body().isSuccess(getApplicationContext(), false)) {
                                                                        showToast("进站成功", true);
                                                                        new Handler().postDelayed(new Runnable() {
                                                                            @Override
                                                                            public void run() {
                                                                                TrackInActivity.this.finish();
                                                                            }
                                                                        }, 2000);
                                                                    }
                                                                }

                                                                @Override
                                                                public void onError(Response<BaseResponseBean<?>> response) {
                                                                    super.onError(response);
                                                                    loadError(response.getException(), "EUIT.EUITTRACKIN");
                                                                }
                                                            });
                                                        }
                                                    }
                                                }

                                                @Override
                                                public void afterTextChanged(Editable s) {

                                                }
                                            });
                                        }
                                        break;
                                    default:
                                        tvCarryId.setText("");
                                        llProviderItem.setVisibility(View.GONE);
                                        llGoodsShelves.setVisibility(View.GONE);
                                        llCheckGoodsShelves.setVisibility(View.GONE);
                                        llTestGoodsShelves.setVisibility(View.GONE);
                                        llGrade.setVisibility(View.GONE);
                                        break;
                                }
//                                getEuitkjtsQuery(s.toString());
                                conutbars = Integer.parseInt(response.body().getResponse().getBody().getCONUTBARS());
                                etItemCount.setText(String.valueOf(conutbars));
                            }
                        }

                        @Override
                        public void onError(Response<BaseResponseBean<ResponseQueryBean>> response) {
                            super.onError(response);
                            loadError(response.getException(), "进站LOT查询");
                        }
                    });
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etItemCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
//                if (!s.toString().equals("")){
//                    if (Integer.parseInt(s.toString()) > conutbars){
//                        showToast("输入数量超过装片数量",false);
//                        etItemCount.setText(String.valueOf(conutbars));
//                    }
//                }
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void getEuitkjtsQuery(String lotID){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("LOTID", lotID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestEuitkjtsQuery(jsonObject,new FreeUI_DialogEntityCallBack<BaseResponseBean<LotBean>>(
                new TypeToken<BaseResponseBean<LotBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<LotBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true)) {
                    conutbars = Integer.parseInt(response.body().getResponse().getBody().getCONUTBARS());
                    etItemCount.setText(String.valueOf(conutbars));
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EUITKJTSQUERY");
            }
        });

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
        mLots.remove(i);
        thingList.remove(i);
        thingListAdapter.notifyDataSetChanged();
    }
}
